\CUSProvideModule{ltx}{\cus@d@te}{\cus@versi@n}{wrapping of LaTeX2e kernel}

\ExplSyntaxOn
\cs_if_free:NT \csstring { \cs_new_eq:NN \csstring \cs_to_str:N }
\cs_new_eq:NN \cus@letcs \cs_set_eq:Nc 
\cs_new_eq:NN \Replicate \prg_replicate:nn
\cs_new_protected:Npn \__cus_cmd_set_argument:n { \tl_set:Nn \ProcessedArgument }
\cs_new_protected:Npn \__cus_cmd_set_argument:x { \tl_set:Nx \ProcessedArgument }
\cs_new_protected:Npn \__cus_cmd_replace_argument:nnnn #1#2#3#4
  {
    #1 {#4}
      { \__cus_cmd_set_argument:n {#2} }
      { \__cus_cmd_set_argument:n {#3} }
  }
\cs_new_protected:Npn \__cus_cmd_expand_argument:Nn #1
  { \ExpandArgs { #1 } \__cus_cmd_set_argument:n }
\cs_new_protected:Npn \__cus_cmd_xxpand_argument:nn #1#2
  { 
    \cs_if_exist:cTF { __cus_cmd_xxpand_argument_#1:n } 
      { \use:c { __cus_cmd_xxpand_argument_#1:n } {#2} }
      { \msg_error:nnx { cus } { cmd-xxpand-argument } {#1} }
  }
\cs_new:Npn \__cus_cmd_xxpand_argument_c:n { \exp_args:Nc \__cus_cmd_set_argument:n }
\cs_new:Npn \__cus_cmd_xxpand_argument_o:n { \exp_args:No \__cus_cmd_set_argument:n }
\cs_new:Npn \__cus_cmd_xxpand_argument_V:n { \exp_args:NV \__cus_cmd_set_argument:n }
\cs_new:Npn \__cus_cmd_xxpand_argument_v:n { \exp_args:Nv \__cus_cmd_set_argument:n }
\cs_new:Npn \__cus_cmd_xxpand_argument_e:n {              \__cus_cmd_set_argument:x }
\cs_new:Npn \__cus_cmd_xxpand_argument_f:n { \exp_args:Nf \__cus_cmd_set_argument:n }
\cs_new:Npn \__cus_cmd_xxpand_argument_x:n {              \__cus_cmd_set_argument:x }
\cs_new:Npn \__cus_cmd_xxpand_argument_sX:n #1
  { \__cus_cmd_set_argument:x { \text_expand:n {#1} } }
\cs_new:Npn \__cus_cmd_xxpand_argument_sF:n #1
  { \__cus_cmd_set_argument:x { \text_purify:n {#1} } }
\cs_new:Npn \__cus_cmd_xxpand_argument_sP:n #1
  { \protected@edef \ProcessedArgument { \ProcessedArgument } }
\cs_new:Npn \__cus_cmd_xxpand_argument_sS:n #1
  { \__cus_cmd_set_argument:x { \tl_to_str:n {#1} } }
\cs_new_eq:NN \__cus_cmd_xxpand_argument_p:n \__cus_cmd_xxpand_argument_sX:n
\cs_new_protected:Npn \__cus_cmd_regex_replace_argument:w #1 #
  { \tl_trim_spaces_apply:nN {#1} \__cus_cmd_regex_replace_argument_aux:nnnn }
\cs_new:Npn \__cus_cmd_regex_replace_argument_aux:nnnn #1#2#3#4
  {
    \tl_set:Nn \ProcessedArgument {#4}
    \bool_lazy_and:nnTF 
      { \tl_if_single_token_p:n {#1} }
      { \token_if_eq_catcode_p:NN + #1 }
      { \regex_replace_all:nnN }
      { \regex_replace_once:nnN } 
        {#2} {#3} \ProcessedArgument
  }
\cs_new_eq:NN \ReplaceArgumentIf \__cus_cmd_replace_argument:nnnn 
\cs_new_protected:Npn \ReplaceArgumentIfEqual #1
  { \__cus_cmd_replace_argument:nnnn { \tl_if_eq:nnTF {#1} } }
\cs_new_protected:Npn \ReplaceArgumentIfStrEqual #1
  { \__cus_cmd_replace_argument:nnnn { \str_if_eq:nnTF {#1} } }
\cs_new_protected:Npn \ReplaceArgumentIfMatch #1
  { \__cus_cmd_replace_argument:nnnn { \regex_match:nnTF {#1} } }
\cs_new_eq:NN \RegexReplaceArgument \__cus_cmd_regex_replace_argument:w 
% \cs_new_protected:Npn \ExpandArgument #1
%   { \exp_args:Nf \__cus_cmd_expand_argument:Nn { \tl_head:f {#1} } }
\cs_new_eq:NN \ExpandArgument \__cus_cmd_xxpand_argument:nn
\msg_new:nnn { cus } { cmd-xxpand-argument } { Unknown~arg~expansion~"#1" }

\cs_new_eq:cN { @dischyph/normal } \@dischyph
\tl_new:N \l__cus_hyphen_color_tl
\cs_new_protected:cpn { @dischyph/opacity } 
  {
    { \l__cus_hyphen_color_tl 
      \discretionary{\char
        \ifnum\hyphenchar\font<\z@\defaulthyphenchar\else\hyphenchar\font\fi}
      {}{} } 
  }
\cs_new_protected:cpn { @dischyph/none } { \discretionary{}{}{} }
\keys_define:nn { cus/typo }
  {
    special-dischyph .choices:nn   = { normal , opacity , none }
      { \cs_set_eq:Nc \@dischyph { @dischyph / \l_keys_choice_tl } } ,
    special-dischyph    .default:n    = opacity ,
    dischyph-opacity .code:n  = {
      \tl_set:Nx \l__cus_tmpa_tl { \fp_min:nn { 1 } { \fp_max:nn {#1} { 0 } } }
      \reverse_if:N \if_dim:w \l__cus_tmpa_tl \p@ = \p@ 
        \tl_set:Nx \l__cus_hyphen_color_tl 
          { \color_select:n {.! \fp_eval:n { \l__cus_tmpa_tl * 100 } } }
        % \tl_set:Nx \l__cus_hyphen_color_tl 
        %   { \transparent { \l__cus_tmpa_tl } } % set in module.pdfmana
      \fi:
    } ,
    dischyph-opacity .initial:n = 0.4 ,
  }

\cs_new:Npn \numberfixedwidth #1#2#3#4 % width, filler, printer, number
  { 
    \exp_args:Nee \__cus_number_fill:nnn
      { \int_eval:n {#1} } { \exp_args:Nne \use:n {#3} { \int_eval:n {#4} } } {#2} 
  }
\cs_new:Npn \numberzerofill #1 { \numberfixedwidth {#1} { 0 } \use:n }
\cs_new:Npn \__cus_number_fill:nnn #1 % width, number-printed, filler
  { 
    \exp_args:Nee \__cus_number_fill:nnnn 
      { \if_int_compare:w #1 > \c_zero_int + \else: - \fi: }
      { \if_int_compare:w #1 < \c_zero_int \use_i:nn \fi: #1 }
  }
\cs_new:Npn \__cus_number_fill:nnnn #1#2#3#4 % sign, abs-width, number-printed, filler
  {
    \if:w + #1 \exp_after:wN \use:nn \else: \exp_after:wN \use_ii_i:nn \fi:
      { \prg_replicate:nn { \int_max:nn { #2 - \tl_count:n {#3} } { 0 } } {#4} } {#3}
  }

\cs_new:Npn \__cus_date_normalize:n #1
  { \__cus_date_normalize_auxi:w #1 / / / \s__cus_stop }
\cs_new:Npn \__cus_date_normalize_auxi:w #1/#2/#3/#4 \s__cus_stop
  {
    \tl_if_empty:nTF {#4}
      { \__cus_date_normalize_auxii:w #1 - - - \s__cus_stop }
      { \__cus_date_normalize_auxiii:nnn {#1} {#2} {#3} }
  }
\cs_new:Npn \__cus_date_normalize_auxii:w #1-#2-#3-#4 \s__cus_stop
  { \__cus_date_normalize_auxiii:nnn {#1} {#2} {#3} }
\cs_new:Npn \__cus_date_normalize_auxiii:nnn #1#2#3
  {
    \elkernel_exp:w
      {
        \int_eval:n {#1+0} /
        \if_int_compare:w \int_eval:n {#2+0} < \c__cus_ten_int 0 \fi: \int_eval:n {#2+0} /
        \if_int_compare:w \int_eval:n {#3+0} < \c__cus_ten_int 0 \fi: \int_eval:n {#3+0}
      }
  }
\ExplSyntaxOff


\protected\def\zkern{\kern\z@}
\let\@EA\expandafter
\def\@EAEA{\@EA\@EA}
\def\@EAEAEA{\@EA\@EA\@EA}
\def\@EAEAEAEAEA{\@EA\@EAEAEA\@EA}
\protected\def\lo#1{\ifmmode\@EA\@firstoftwo\else\@EA\@secondoftwo\fi
  {{}_{#1}}{\textsubscript{{#1}}}}
\protected\def\hi#1{\ifmmode\@EA\@firstoftwo\else\@EA\@secondoftwo\fi
  {{}^{#1}}{\textsuperscript{{#1}}}}
\protected\def\lohi#1#2{%
  \ifmmode\@EA\@firstoftwo\else\@EA\@secondoftwo\fi 
  {_{#1}^{#2}}
  {\m@th 
    $\relax 
    _{\hbox{{\fontsize\sf@size\sf@size\selectfont #1}}}%
    ^{\hbox{{\fontsize\sf@size\sf@size\selectfont #2}}}%
    $}}

\NewDocumentCommand\makelapbox{O{}O{c}>{\ReplaceArgumentIfStrEqual{s}{c}{#3}}O{#2}m}
  {\leavevmode@ifvmode\@nameuse{#3lap}{%
    \IfBlankTF{#1}{\mbox}{\makebox[{#1}][{#2}]}{#4}}}
\NewDocumentCommand\parlapbox{O{c}O{}O{s}O{c}m+m}
  {\leavevmode@ifvmode\@nameuse{#4lap}{\parbox[{#1}][{#2}][{#3}]{#5}{#6}}}

\newenvironment{enumlist}[5][]
  {\list{#1}{%
    \leftmargin=\dimeval{#2+\z@}\relax
    \itemindent=\dimeval{#3+\z@}\relax
    \listparindent=\itemindent
    \labelsep=\dimeval{#4+\z@}\relax
    \rightmargin=\dimeval{#5+\z@}\relax}}
  {\endlist}
\newenvironment{enumlist*}[5][]
  {\list{#1}{%
    \leftmargin=\dimeval{#2+\z@}\relax
    \itemindent=\dimeval{#3+\z@}\relax
    \listparindent=\itemindent
    \labelsep=\dimeval{#4+\z@}\relax
    \rightmargin=\dimeval{#5+\z@}\relax
    \topsep\z@ \partopsep\z@ \itemsep\z@ \parsep\z@ \parskip\z@}}
  {\endlist}


\def\cus@mathdispatch#1#2{%
  \mathchoice{#1\tf@size{#2}}{#1\tf@size{#2}}{#1\sf@size{#2}}{#1\ssf@size{#2}}}

\def\cusemojilowerratio{1/7}
\def\cusemojitotalratio{8/9}
\protected\def\cus@emoji@pic#1#2{\leavevmode@ifvmode
  \lower\dimexpr #1\p@*\cusemojitotalratio*\cusemojilowerratio
    \hbox{\includegraphics[height=\dimeval{#1\p@*\cusemojitotalratio}]{#2}}}
\def\cus@emoji@math#1{\cus@mathdispatch{\cus@emoji@pic}{#1}}
\protected\def\cusemoji#1{{\ifmmode\cus@emoji@math{#1}\else\cus@emoji@pic\f@size{#1}\fi}}

\protected\def\cusautosize#1{\ifmmode
    {\setbox\z@\hbox{\m@th $\relax #1$}\cus@mathautosize}\@EA\@gobble\fi
  {\leavevmode@ifvmode #1}}
\def\cus@mathautosize{\mathchoice
  {\copy\z@}{\copy\z@}{\cus@scale@box\z@\defaultscriptratio\defaultscriptratio \copy\z@}
  {\cus@scale@box\z@\defaultscriptscriptratio\defaultscriptscriptratio \box\z@}}

\ExplSyntaxOn
\cs_new_eq:NN \cus@scale@box \box_scale:Nnn
\cs_new_protected_nopar:Npn \cus@char@pic #1#2#3#4 % pic ht, dp, wd, file
  {
    \mode_leave_vertical:
    \tl_set:Nx \l__cus_tmpa_tl
      {
        [ \tl_if_empty:nF {#1} { height = \dim_eval:n { #1 } , }
          \tl_if_empty:nF {#3} { width  = \dim_eval:n { #3 } } ]
        \cs:w exp_not:n \use:e { \cs_end: {#4} }
      }
    \tl_if_empty:nTF {#2} { \use:n } { \box_move_down:nn {#2} }
      { \hbox:n { \exp_after:wN \includegraphics \l__cus_tmpa_tl } }
  }
\ExplSyntaxOff 
\NewDocumentCommand\cuscharpictext
  {O{\f@size\p@} D<>{\cusemojilowerratio} O{(#1)*#2} O{} m}
  {\cus@char@pic{#1}{#3}{#4}{#5}}


\long\def\IfGraphicsExists#1#2#3{%
  \cus@getgraphicsname\@curr@file{\cus@filename{#1}}%
  \@ifundefined{@curr@file}{#3}{#2}}
\ekeysdeclarecmd\InputIfGraphicsExists{ &s @o o m }
  {\cus@getgraphicsname\@curr@file{\cus@filename{#4}}%
    \ifx\@curr@file\relax\else
      \expanded{\noexpand\includegraphics#1
        \IfValueT{#2}{\IfValueTF{#3}{\unexpanded{[#2][#3]}}{[{\unexpanded{#2}}]}}%
        {\cus@filename\@curr@file}}%
    \fi}
\catcode`\*=11
% copy of \Ginclude@graphics of graphics.sty 
\def\cus@getgraphicsname#1#2{%
  \edef\Gin@extensions{\detokenize\expandafter{\Gin@extensions}}%
  \begingroup
  \let\input@path\Ginput@path
  \Gin@set@curr@file{#2}%
  \expandafter\filename@parse\expandafter{\@curr@file}%
  \ifx\filename@ext\Gin@gzext
    \expandafter\filename@parse\expandafter{\filename@base}%
    \ifx\filename@ext\relax
      \let\filename@ext\Gin@gzext
    \else
      \edef\filename@ext{\filename@ext\Gin@sepdefault\Gin@gzext}%
    \fi
  \fi
  \ifx\filename@ext\relax
    \@for\Gin@temp:=\Gin@extensions\do{%
      \ifx\Gin@ext\relax
        \Gin@getbase\Gin@temp
      \fi}%
  \else
    \Gin@getbase{\Gin@sepdefault\filename@ext}%
    \ifnum0%
        \ifx\Gin@ext\relax 1%
        \else \@ifundefined{Gin@rule@\Gin@ext}{1}{0}%
        \fi >0
      \let\Gin@extsaved\Gin@ext
      \let\Gin@savedbase\filename@base
      \let\Gin@savedext\filename@ext
      \let\Gin@ext\relax
      \edef\filename@base{\filename@base\Gin@sepdefault\filename@ext}%
      \let\filename@ext\relax
      \@for\Gin@temp:=\Gin@extensions\do{%
          \ifx\Gin@ext\relax
            \Gin@getbase\Gin@temp
          \fi}%
      \ifx\Gin@ext\relax
        \let\Gin@ext\Gin@extsaved
        \let\filename@base\Gin@savedbase
        \let\filename@ext\Gin@savedext
      \fi
    \fi
  \fi
  \ifx\Gin@ext\relax
    \global\let\@curr@file\relax
  \else
    \@ifundefined{Gin@rule@\Gin@ext}
      {\global\let\@curr@file\relax}
      {\xdef\@curr@file{\Gin@base\Gin@ext}}%
  \fi
  \endgroup \let#1\@curr@file}
\catcode`\*=12

\ExplSyntaxOn
\cs_new_protected:Npn \cus_get_graphics_full_name:nN #1#2
  { 
    \cus_get_graphics_full_name:nNF {#1} #2
      { \tl_set:Nn #2 { \q_no_value } }
  }
\prg_new_protected_conditional:Npnn \cus_get_graphics_full_name:nN #1#2 { T, F, TF }
  {
    \exp_args:NNe \cus@getgraphicsname #2 { \cus@filename {#1} }
    \token_if_eq_meaning:NNTF #2 \scan_stop:
      { \prg_return_false: } { \prg_return_true: }
  }

\tl_if_exist:NF \input@path { \tl_new:N \input@path }
\ekeysdeclarecmd \setinputpath { t+ m }
  {
    \bool_if:nF {#1} { \tl_clear:N \input@path }
    \clist_map_inline:nn {#2}
      { \tl_put_right:Nx \input@path { { \cus@filename {##1} } } }
  }
\ekeysdeclarecmd \setgraphicspath { t+ m }
  {
    \bool_if:nF {#1} { \tl_clear:N \Ginput@path }
    \clist_map_inline:nn {#2}
      { \tl_put_right:Nx \Ginput@path { { \cus@filename {##1} } } }
  }
\ExplSyntaxOff
